﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoAn.DTO;
using DoAn.BUS;
using System.Data;

namespace DoAn
{
    public partial class DoiMatKhau : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = KiemTraMatKhau();
        }

        public bool KiemTraMatKhau()
        {
            if(Request.QueryString["tentk"]==null)
            {
                Response.Redirect("TrangChu.aspx");
            }
            else
            {                
                TaiKhoanDTO taik = new TaiKhoanDTO();
                taik.TenTaiKhoan = Request.QueryString["tentk"].ToString();
                DataTable dt = new DataTable();
                dt = TaiKhoanBUS.loadTaiKhoan(taik);
                if (dt.Rows[0]["MatKhau"].ToString() == TaiKhoanBUS.getMd5Hash(txtMatKhauCu.Text))
                {
                    return true;
                }
            }
            return false;
        }

        protected void bntDoiMatKhau_Click(object sender, EventArgs e)
        {
            if (KiemTraMatKhau())
            {
                TaiKhoanDTO tk=new TaiKhoanDTO();
                tk.TenTaiKhoan=Request.QueryString["tentk"].ToString();
                tk.MatKhau= TaiKhoanBUS.getMd5Hash(txtXacNhanMatKhau.Text);
                TaiKhoanBUS.doiMatKhau(tk);
            }
        }
    }
}